<%--
  Created by IntelliJ IDEA.
  User: 莫
  Date: 2017/12/20
  Time: 21:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    <link href="css/bootstrap.min.css" rel="stylesheet">

</head>
<body>
<nav class="navbar navbar-default">
    <div class="navbar-header">
        <button type="button" class="btn btn-navbar navbar-toggle" data-toggle="collapse" data-target=".navbar-exl-collapse"><i class="fa fa-bars"></i></button>
        <a class="navbar-brand" href="#">后台管理</a>
    </div>
    <div class="collapse navbar-collapse navbar-exl-collapse">
        <ul class="nav navbar-nav">
            <li class="active"><a href="#">新闻 <span class="sr-only">(current)</span></a></li>
            <li><a href="#">Link</a></li>
        </ul>
    </div>>
</nav>

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-2">
            <a href="NewsManage.jsp" class="list-group-item active">新闻管理</a>
            <a href="NewsRelease.jsp" class="list-group-item">新闻发布</a>
        </div>
        <div class="col-sm-10">
            <div class="panel panel-default">
                <div class="panel-heading">
                    搜索
                </div>
                <div class="panel-body">
                    <form role="form" class="form-inline" action="">
                        <div class="form-group">
                            <label for="name">名称</label>
                            <input type="text" class="form-control" id="name" placeholder="请输入名称">
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-default">开始搜索</button>
                        </div>
                    </form>
                </div>
            </div>
            <!--
                列表展示
            -->
            <div class="table-responsive">
                <table class="table table-striped " id="table">
                    <thead>
                    <tr>
                        <th>编号</th>
                        <th>标题</th>
                        <th>作者</th>
                        <th>时间</th>
                        <th>点击率</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>

                    </tbody>
                </table>
            </div>
            <ul class="pagination" style="float: right;">
                <li><a href="#">&laquo;</a>
                </li>
                <li class="active"><a href="#">1</a>
                </li>
                <li class="disabled"><a href="#">2</a>
                </li>
                <li><a href="#">3</a>
                </li>
                <li><a href="#">4</a>
                </li>
                <li><a href="#">5</a>
                </li>
                <li><a href="#">&raquo;</a>
                </li>
            </ul>
        </div>
    </div>
</div>
<script src="js/jquery-2.1.1.min.js"></script>
<script>
    var xmlhttp;
    function getUrlParams(name)
    {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return r[2]; return "";
    }
    var name = getUrlParams("name");
    //获取cookie值
    function getCookie(name)
    {
        //document.cookie.setPath("/");
        var arr, reg = new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg))
        {
            return unescape(arr[2]);
        }
        else
        {
            return null;
        }
    }

    window.onload = function () {
        xmlhttp=new XMLHttpRequest();
        xmlhttp.open("POST","TableHtml",false);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("FN=1");

        var news = JSON.parse(xmlhttp.responseText);
        var NewsList = news.NewsList;
        // var name = getUrlParams("name");

        //Cookie接收
        var CurUserName=getCookie("CurUserName");//另外一个页面用getCookie来获取值  
        console.log(CurUserName);

        var table = document.getElementById("table");
        for (var i = 0;i<NewsList.length;i++)
        {
            var username = NewsList[i].Name;
            username=username.replace(/^\s+|\s+$/g,"");
            // console.log(username);
            if (CurUserName==username) {
                var row = table.insertRow(table.rows.length);
                var ID = row.insertCell(0);
                ID.innerHTML = NewsList[i].Id;
                var Head = row.insertCell(1);
                Head.innerHTML = NewsList[i].Headline;
                var Name = row.insertCell(2);
                Name.innerHTML = NewsList[i].Name;
                var Time = row.insertCell(3);
                Time.innerHTML = NewsList[i].UpTime;
                var Check = row.insertCell(4);
                if(NewsList[i].Check == 1) {
                    Check.innerHTML = "已通过";
                }
                else if(NewsList[i].Check == 2){
                    Check.innerHTML = "未通过";
                }
                else  Check.innerHTML = "审核中";
                var OpenNews = row.insertCell(5);
                OpenNews.innerHTML = "<button title='"+NewsList[i].Id+"'>删除</button>";

            }

        }
        // 删除
        var buts = document.getElementsByTagName("button");
        console.log(buts);
        var len_but = buts.length;
        for(var i = 2;i<len_but;i++)
        {
            var but = buts[i];
            but.onclick = function () {
                var id = this.title;
                xmlhttp=new XMLHttpRequest();
                xmlhttp.open("POST","DeleteHtml",false);
                xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                xmlhttp.send("id="+id);

                window.location.reload();
            }
        }

        // var news=[{"uid":"2688","uname":"*江苏省南菁高级中学 022","sum":274.23},{"uid":"2689","uname":"*江阴国际会展中心管理有限公司 024","sum":0},{"uid":"2686","uname":"江苏申利实业股份有限公司 001","sum":7917.94},{"uid":"2715","uname":"江阴市吉达针纺有限公司 115","sum":41.39},{"uid":"2688","uname":"*江苏省南菁高级中学 022","sum":274.23}]
        //
        // for (var i = 0;i<news.length;i++)
        // {
        //     var row = table.insertRow(table.rows.length);
        //     var ID = row.insertCell(0);
        //     ID.innerHTML = news[i].uid;
        //     var Head = row.insertCell(1);
        //     Head.innerHTML = news[i].uname;
        //     var Name = row.insertCell(2);
        //     Name.innerHTML = news[i].sum;
        // }
    }
</script>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
